package org.ilc.sample.executor;

import org.ilc.Bootstrap;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author malongbo
 */
public class QueueExecutor {
    private ThreadPoolExecutor threadPool = null;

    public QueueExecutor () {
        int min = 5;
        int max = 50;
        try {
            min = Integer.parseInt (Bootstrap.getContext ().getConfig ().get ("push.cache.read.corepoolsize"));
            max = Integer.parseInt (Bootstrap.getContext ().getConfig ().get ("push.cache.read.maxpoolsize"));
        } catch (Exception e) {
            min = 5;
            max = 50;

        }
        initPool (min, max);
    }
    public QueueExecutor (int minThreads,int maxThreads) {
       initPool (minThreads, maxThreads);
    }

    private void initPool (int minThreads,int maxThreads) {
        this.threadPool = new ThreadPoolExecutor(minThreads, maxThreads, 0L,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable> ());
    }
    public QueueExecutor(ThreadPoolExecutor threadPool){
        this.threadPool=threadPool;
    }

    public void read (String account) {
        threadPool.execute (new Reader(account));
    }
    class Reader implements Runnable {
        private String account;

        public Reader (String account) {
            this.account = account;
        }

        @Override
        public void run() {
           ReadTask.read (account);
        }
    }
}
